package org.dromara.web.controller;

import java.util.List;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.TQuestion;
import org.dromara.system.mapper.TQuestionMapper;
import org.dromara.web.domain.TSurvey;
import org.dromara.web.mapper.TSurveyMapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.web.domain.vo.TSurveyVo;
import org.dromara.web.domain.bo.TSurveyBo;
import org.dromara.web.service.ITSurveyService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

import static org.dromara.common.satoken.utils.LoginHelper.getTenantId;

/**
 * 问卷表单
 *
 * @author Lion Li
 * @date 2024-10-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/survey")
public class TSurveyController extends BaseController {

    private final ITSurveyService tSurveyService;

    @Resource
    private TSurveyMapper tSurveyMapper;

    @Resource
    private TQuestionMapper tQuestionMapper;

    /**
     * 查询问卷表单列表
     */
    @SaCheckPermission("web:survey:list")
    @GetMapping("/list")
    public TableDataInfo<TSurveyVo> list(TSurveyBo bo, PageQuery pageQuery) {
        return tSurveyService.queryPageList(bo, pageQuery);
    }


    @GetMapping("/get")
    public R<TSurveyVo> get() {
        String tenantId = StpUtil.getLoginDevice();
        List<TSurvey> tSurveys =  tSurveyMapper.selectList(new QueryWrapper<TSurvey>().eq("enable",1).eq("tenant_id",tenantId));
        if(tSurveys.size() > 0){
            TSurvey tSurvey = tSurveys.get(0);
            Integer surveyId = tSurvey.getSurveyId();
            List<TQuestion> tQuestions = tQuestionMapper.selectList(new QueryWrapper<TQuestion>().eq("survey_id",surveyId).eq("tenant_id",tenantId).orderByAsc("order_index"));
            TSurveyVo tSurveyVo = new TSurveyVo();
            tSurveyVo.setTitle(tSurvey.getTitle());
            tSurveyVo.setEnable(tSurvey.getEnable());
            tSurveyVo.setDescription(tSurvey.getDescription());
            tSurveyVo.setSurveyId(tSurvey.getSurveyId());
            tSurveyVo.setTQuestions(tQuestions);
            return R.ok(tSurveyVo);
        }else {
            return R.fail("没有找到有效的问卷,请联系管理员");
        }
    }

    /**
     * 导出问卷表单列表
     */
    @SaCheckPermission("web:survey:export")
    @Log(title = "问卷表单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TSurveyBo bo, HttpServletResponse response) {
        List<TSurveyVo> list = tSurveyService.queryList(bo);
        ExcelUtil.exportExcel(list, "问卷表单", TSurveyVo.class, response);
    }

    /**
     * 获取问卷表单详细信息
     *
     * @param surveyId 主键
     */
    @SaCheckPermission("web:survey:query")
    @GetMapping("/{surveyId}")
    public R<TSurveyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer surveyId) {
        return R.ok(tSurveyService.queryById(surveyId));
    }

    /**
     * 新增问卷表单
     */
    @SaCheckPermission("web:survey:add")
    @Log(title = "问卷表单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TSurveyBo bo) {
        return toAjax(tSurveyService.insertByBo(bo));
    }

    /**
     * 修改问卷表单
     */
    @SaCheckPermission("web:survey:edit")
    @Log(title = "问卷表单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TSurveyBo bo) {
        return toAjax(tSurveyService.updateByBo(bo));
    }

    /**
     * 删除问卷表单
     *
     * @param surveyIds 主键串
     */
    @SaCheckPermission("web:survey:remove")
    @Log(title = "问卷表单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{surveyIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] surveyIds) {
        return toAjax(tSurveyService.deleteWithValidByIds(List.of(surveyIds), true));
    }
}
